package com.btm.dorisinfo.controller;

import com.btm.dorisinfo.model.TableInfo;
import com.btm.dorisinfo.service.DorisInfoExtractorService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Doris表结构信息控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/doris")
public class DorisInfoController {

    @Autowired
    private DorisInfoExtractorService dorisInfoExtractorService;

    /**
     * 提取指定数据库的表结构信息
     * @param databaseName 数据库名称
     * @return 表结构信息列表
     */
    @GetMapping("/tables/{databaseName}")
    public ResponseEntity<List<TableInfo>> getTableInfo(@PathVariable String databaseName) {
        log.info("接收到REST请求：获取数据库[{}]的表结构信息", databaseName);
        List<TableInfo> tableInfoList = dorisInfoExtractorService.extractTableInfo(databaseName);
        return ResponseEntity.ok(tableInfoList);
    }

    /**
     * 提取指定数据库的表结构信息并存储到MeiliSearch
     * @param databaseName 数据库名称
     * @return 处理结果
     */
    @GetMapping("/tables/{databaseName}/store")
    public ResponseEntity<Map<String, Object>> extractAndStore(@PathVariable String databaseName) {
        log.info("接收到REST请求：提取并存储数据库[{}]的表结构信息", databaseName);
        
        // 提取表结构信息
        List<TableInfo> tableInfoList = dorisInfoExtractorService.extractTableInfo(databaseName);
        
        // 存储到MeiliSearch
        String result = dorisInfoExtractorService.storeToMeiliSearch(tableInfoList);
        
        Map<String, Object> response = new HashMap<>();
        response.put("message", result);
        response.put("tableCount", tableInfoList.size());
        
        return ResponseEntity.ok(response);
    }
}